//
// Created by lenovo on 2024/7/12.
//

#ifndef TEYVATSURVIVAL_GAME_WEAPON_WEAPON_H
#define TEYVATSURVIVAL_GAME_WEAPON_WEAPON_H
#include "../Obj/GameObj.h"
#include <vector>
#ifdef WEAPON_PROPERTYPE

class Bullet {
public:
    Vector2f position;
    sf::CircleShape circle;

    Bullet() {
        circle = sf::CircleShape(50);
        circle.setFillColor(sf::Color::Red);
    }

    void setPosition(Vector2f p) {
        this->position = p;
        circle.setPosition(p.x, p.y);
    }

    Vector2f getPosition() const {
        return position;
    }

    void draw(sf::RenderWindow& window) {
        window.draw(circle);
    }
};

class Player {
public:
    Animation anima;
    Vector2f position;
    Bullet bullet;
    float bulletAngle;
    float bulletOrbitRadius;

    Player()
            : bulletAngle(0.0f), bulletOrbitRadius(80.0f) {
        anima = AnimationBuilder()
                .addPath("../resource/img/player_left_0.png")
                .addPath("../resource/img/player_left_1.png")
                .addPath("../resource/img/player_left_2.png")
                .addPath("../resource/img/player_left_3.png")
                .addPath("../resource/img/player_left_4.png")
                .addPath("../resource/img/player_left_5.png")
                .position({300, 400})
                .interval(1)
                .opacity(0.8f)
                .loop(true)
                .pause()
                .play()
                .scale({150,150})
                .build();
        position = anima.getPosition();
    }

    void update(float deltaTime) {
        anima.update(deltaTime);
//        position = anima.getPosition();
//
//        // 获取玩家动画的尺寸
//        sf::Vector2f playerSize(anima.getScale().x, anima.getScale().y);
//
//        // 计算玩家图像的中心点
//        sf::Vector2f playerCenter(position.x + playerSize.x / 2, position.y + playerSize.y / 2);
//
//        // 更新子弹角度
//        bulletAngle += deltaTime; // 控制子弹的旋转速度
//
//        // 计算子弹位置
//        float bulletX = playerCenter.x + bulletOrbitRadius * cos(bulletAngle) - bullet.circle.getRadius();
//        float bulletY = playerCenter.y + bulletOrbitRadius * sin(bulletAngle) - bullet.circle.getRadius();
//
//        // 更新子弹位置
//        bullet.setPosition(Vector2f(bulletX, bulletY));
        auto p1 = anima.getPosition();
        auto p2 = p1 + Vector2f ({
            anima.getScale().x / 2,
            anima.getScale().y / 2
        });

        bulletAngle += deltaTime * 50; // 控制子弹的旋转速度
        auto p3 = p2 +
                bulletOrbitRadius *
                    Vector2f ({
                        cos(bulletAngle),
                        sin(bulletAngle)
                    });
//        cout << bullet.circle.getScale().x << endl ;
        auto p4 = p3 - Vector2f ({
            bullet.circle.getRadius(),
            bullet.circle.getRadius()
        });
//        bullet.circle.
        bullet.setPosition(p4);
    }

    void draw(sf::RenderWindow& window) {
        window.draw(anima);
        bullet.draw(window);
    }
};

#endif
namespace Game::Obj::Wea{
    using namespace Game::Obj;
    /**
     * @note
     * weapon是bullet的管理类
     */
    interface Weapon{
    public :
        virtual void update(float delta) = 0 ;
    };

    class BulletOrbitWeapon : public Weapon{
    public:
        Player *player;
        std::vector<Bullet*> bullets;  // 子弹集合
        float radius; // 子弹绕玩家旋转的半径
        float bulletSpeed; // 子弹的速度
        float bulletAngle ;
        int bulletNum; // 子弹数量

        // 构造函数
        BulletOrbitWeapon(Player *player) : player(player), bulletNum(3) , bulletAngle(0) , radius(100) , bulletSpeed(1){
            // 初始化子弹
            load();

        }

        void load(){
            for (int i = 0; i < bulletNum; i++) {
                auto bull = new Bullet;
                bull->shape = new sf::CircleShape(10);
                bull->shape->setFillColor(sf::Color::Blue);
                bullets.push_back(bull);
            }
        }

        // 更新子弹
        void updateBullets(float delta) {

            for (int i = 0 ; i < bulletNum ; i ++){
                auto p1 = player->getPosition();
                auto p2 = p1 + Vector2f ({
                                                 player->getScale().x / 2,
                                                 player->getScale().y / 2
                                         });

                bulletAngle += delta * bulletSpeed; // 控制子弹的旋转速度
                auto p3 = p2 +
                          radius *
                          Vector2f ({
                                            static_cast<float>(cos(bulletAngle + i *( 2 * M_PI / bulletNum))),
                                            static_cast<float>(sin(bulletAngle + i *( 2 * M_PI / bulletNum)))
                                    });
//        cout << bullet.circle.getScale().x << endl ;
                auto p4 = p3 - Vector2f ({
                                                 bullets[i]->shape->getRadius(),
                                                 bullets[i]->shape->getRadius()
                                         });
//        bullet.circle.
                bullets[i]->setPosition(p4);
            }
        }

        // 更新武器状态
        void update(float delta) override {
            // 更新子弹
            updateBullets(delta);
        }

//        // 绘制武器
//        void draw(sf::RenderWindow& window) {
//            // 绘制每个子弹
//            for (Bullet* bullet : bullets) {
//                bullet->draw(window);
//            }
//        }
    };



}

#endif //TEYVATSURVIVAL_GAME_WEAPON_WEAPON_H
